//
//  NSMatchViewController.m
//  Meboo
//
//  Created by kyo on 2024/5/9.
//

#import "NSMatchViewController.h"
#import "NSConversationViewController.h"
#import "NSModelDetailViewController.h"
@interface NSMatchViewController ()
@property (nonatomic, strong) UIImageView *modelBackImageView;
@property (nonatomic, strong) UIVisualEffectView *effectView;
//零零碎碎的小花图片
@property (nonatomic, strong) UIImageView *starsImageView;
//MATCHED!
@property (nonatomic, strong) UIImageView *titleImageView;
//She likes you back!
@property (nonatomic, strong) UILabel *subTitleLab;
@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, strong) UIImageView *myIconImageView;
@property (nonatomic, strong) UIImageView *modelIconImageView;
@property (nonatomic, strong) UIImageView *likeImageView;

@property (nonatomic, strong) NSMutableArray *emojiTitleArr;

@property (nonatomic, strong) UITextField *messageTF;
@property (nonatomic, strong) UIButton *sendBtn;

@property (nonatomic, assign) CGFloat wid;
@property (nonatomic, assign) CGFloat perWid;
@end

@implementation NSMatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = UIColor.clearColor;
    [self setupUI];
    self.perWid = self.wid/50.0;
    
    NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
    for (BaseViewController *vc in marr) {
        if ([vc isKindOfClass:[NSModelDetailViewController class]]) {
            [marr removeObject:vc];
            break;
        }
    }
    self.navigationController.viewControllers = marr;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.myIconImageView.hidden = NO;
    self.modelIconImageView.hidden = NO;
    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(startAnimation:)
                                   userInfo:nil
                                    repeats:YES];
}

#pragma mark - Public Metohd
- (void)setModel:(MBWLMTopicModel *)model {
    _model = model;
    [self.modelBackImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl]];
    [self.modelIconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl]];
    [self.myIconImageView sd_setImageWithURL:[NSURL URLWithString:[MBUserAccount defaultAccount].avatarUrl]];
}

#pragma mark - Action
- (void)closeAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)sendAction {
    NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
    if ([[self.messageTF.text stringByTrimmingCharactersInSet:set] length] == 0) {
        // 字符串全是空格
        return;
    }
    if (self.messageTF.text.length == 0) {
        return;
    }
    
    NSConversationViewController *conversationVC = [[NSConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:self.model.friendUserCode ?: self.model.userCodeFriend];
    conversationVC.matchedStr = self.messageTF.text;
    conversationVC.isFromWLM = YES;
    [self.currentViewController.navigationController pushViewController:conversationVC animated:YES];
}

- (void)emojiAction:(UIButton *)button {
    NSConversationViewController *conversationVC = [[NSConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:self.model.friendUserCode ?: self.model.userCodeFriend];
    conversationVC.matchedStr = self.emojiTitleArr[button.tag];
    conversationVC.isFromWLM = YES;
    [self.currentViewController.navigationController pushViewController:conversationVC animated:YES];
}

- (void)startAnimation:(NSTimer *)timer {
    self.wid -= self.perWid;
    self.likeImageView.alpha += 0.02;
    if (self.likeImageView.alpha >= 1) {
        self.wid = 0;
        [timer invalidate];
    }
    
    [self.myIconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_centerX).offset(self.wid);
    }];
    [self.modelIconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_centerX).offset(-self.wid);
    }];
}

#pragma mark - UI
- (void)setupUI {
    
    [self.view addSubview:self.modelBackImageView];
    [self.modelBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.starsImageView];
    [self.starsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(ScreenWidth/375*529);
    }];
    
    [self.view addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-20));
        make.top.mas_equalTo(MBHeightScale(20)+StatusBarHeight);
        make.width.height.mas_offset(MBHeightScale(20));
    }];
    
    [self.view addSubview:self.titleImageView];
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(53));
        make.width.mas_offset(ScreenWidth-MBHeightScale(80));
    }];
    
    [self.view addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.titleImageView.mas_bottom).offset(MBHeightScale(8));
    }];
    
    //中间图片和动效
    self.wid = ScreenWidth/2+MBWidthScale(15);
    [self.view addSubview:self.myIconImageView];
    [self.myIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view);
        make.width.mas_equalTo(MBWidthScale(136));
        make.height.mas_equalTo(MBWidthScale(136)/136*226);
        make.left.equalTo(self.view.mas_centerX).offset(self.wid);
    }];
    
    [self.view addSubview:self.modelIconImageView];
    [self.modelIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(MBHeightScale(-40));
        make.width.mas_equalTo(MBWidthScale(136));
        make.height.mas_equalTo(MBWidthScale(136)/136*226);
        make.right.equalTo(self.view.mas_centerX).offset(-self.wid);
    }];
    
    [self.view addSubview:self.likeImageView];
    [self.likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.height.mas_equalTo(MBHeightScale(60));
    }];
    
    
    //底部输入
    [self.view addSubview:self.sendBtn];
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-75)-TabbarSafeBottomMargin);
        make.right.mas_equalTo(MBWidthScale(-30));
        make.width.mas_equalTo(MBWidthScale(60));
        make.height.mas_equalTo(MBHeightScale(40));
    }];
    
    [self.view addSubview:self.messageTF];
    [self.messageTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-75)-TabbarSafeBottomMargin);
        make.left.mas_equalTo(MBWidthScale(30));
        make.height.mas_equalTo(MBHeightScale(40));
        make.right.equalTo(self.sendBtn.mas_left).offset(MBWidthScale(-10));
    }];
    
    //emoji
    CGFloat wid = (ScreenWidth-MBHeightScale(36*4))/5;
    for (int i = 0; i<self.emojiTitleArr.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        [button setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [button setTitle:self.emojiTitleArr[i] forState:0];
        button.titleLabel.font = MBHelFontSizeScale(30);
        [button addTarget:self action:@selector(emojiAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.messageTF.mas_top).offset(MBHeightScale(-26));
            make.width.height.mas_equalTo(MBHeightScale(36));
            make.left.mas_equalTo(wid+(MBHeightScale(36)+wid)*i);
        }];
    }
    
    //旋转
    self.modelIconImageView.transform = CGAffineTransformMakeRotation(-M_PI*7.5/180);
    self.myIconImageView.transform = CGAffineTransformMakeRotation(M_PI*15/180);
}

#pragma mark - Setter && Getter

- (UIImageView *)modelBackImageView {
    if (!_modelBackImageView) {
        _modelBackImageView = [UIImageView new];
        _modelBackImageView.contentMode = UIViewContentModeScaleAspectFill;
        _modelBackImageView.clipsToBounds = YES;
    }
    return _modelBackImageView;
}


- (UIVisualEffectView *)effectView {
    if (!_effectView) {
        UIBlurEffect *blurEffrct = [UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark];
        _effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
        _effectView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.3];
        _effectView.alpha = 0.95;
    }
    return _effectView;
}

- (UIImageView *)starsImageView {
    if (!_starsImageView) {
        _starsImageView = [UIImageView new];
        _starsImageView.image = ImageWithName(@"wlm_matched_starBack");
    }
    return _starsImageView;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc] init];
        [_closeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_closeBtn setImage:ImageWithName(@"flash_chat_close") forState:0];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIImageView *)titleImageView {
    if (!_titleImageView) {
        _titleImageView = [UIImageView new];
        _titleImageView.image = ImageWithName(@"wlm_matched");
    }
    return _titleImageView;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel new];
        _subTitleLab.text = @"She likes you back!";
        _subTitleLab.textAlignment = NSTextAlignmentCenter;
        _subTitleLab.font = MBHelFontSizeScale(20);
        _subTitleLab.textColor = HEXS(@"F04D89");
    }
    return _subTitleLab;
}


- (UIImageView *)modelIconImageView {
    if (!_modelIconImageView) {
        _modelIconImageView = [UIImageView new];
        _modelIconImageView.hidden = YES;
        _modelIconImageView.clipsToBounds = YES;
        _modelIconImageView.layer.cornerRadius = MBHeightScale(16);
        _modelIconImageView.layer.masksToBounds = YES;
        _modelIconImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _modelIconImageView;
}

- (UIImageView *)myIconImageView {
    if (!_myIconImageView) {
        _myIconImageView = [UIImageView new];
        _myIconImageView.hidden = YES;
        _myIconImageView.clipsToBounds = YES;
        _myIconImageView.layer.cornerRadius = MBHeightScale(16);
        _myIconImageView.layer.masksToBounds = YES;
        _myIconImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _myIconImageView;
}

- (UIImageView *)likeImageView {
    if (!_likeImageView) {
        _likeImageView = [UIImageView new];
        _likeImageView.image = ImageWithName(@"wlm_matched_like");
        _likeImageView.alpha = 0;
    }
    return _likeImageView;
}

- (UITextField *)messageTF {
    if (!_messageTF) {
        _messageTF = [UITextField new];
        // 创建占位符文本
        NSString *placeholderText = @"Go ahead and flirt boldly!";
        NSAttributedString *placeholderAttributedString = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{
            NSForegroundColorAttributeName : HEXS(@"BBBBBB"), // 设置字体颜色为蓝色
            NSFontAttributeName : MBHelFontSizeScale(15) // 设置字体大小和样式
        }];
        _messageTF.attributedPlaceholder = placeholderAttributedString;
        _messageTF.textColor = HEXS(@"ffffff");
        _messageTF.font = MBHelFontSizeScale(15);
        _messageTF.layer.borderColor = HEXS(@"ffffff").CGColor;
        _messageTF.layer.borderWidth = 1.5;
        _messageTF.layer.cornerRadius = MBHeightScale(20);
        _messageTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 2)];
        _messageTF.leftViewMode = UITextFieldViewModeAlways;
        _messageTF.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, -15, 2)];
        _messageTF.rightViewMode = UITextFieldViewModeAlways;
    }
    return _messageTF;
}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_sendBtn setTitle:@"Send" forState:0];
        [_sendBtn setTitleColor:HEXS(@"ffffff") forState:0];
        _sendBtn.titleLabel.font = MBHelBoldFontSizeScale(14);
        _sendBtn.layer.cornerRadius = MBHeightScale(20);
        [_sendBtn setBackgroundColor:HEXS(@"B92176")];
        [_sendBtn addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

- (NSMutableArray *)emojiTitleArr {
    if (!_emojiTitleArr) {
        _emojiTitleArr = [NSMutableArray arrayWithArray:@[@"👋",@"😉",@"❤️",@"😍"]];
    }
    return _emojiTitleArr;
}
@end
